/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pv1682010.gui;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javax.sql.DataSource;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import pv1682010.Course;
import pv1682010.CourseManager;
import pv1682010.Database;
import pv1682010.Event;
import pv1682010.User;
import pv1682010.UserNotEnrolledException;

/**
 *
 * @author Petr
 */
public class AttendanceTableModel extends AbstractTableModel {

    private CourseManager cm;
    private Course course = null;
    private List<User> users;
    private List<Event> events;
    private Map<Event, List<User>> attendees;
    private ResourceBundle translations;

    public AttendanceTableModel() {
        DataSource ds = new Database();
        cm = new CourseManager(ds);
        translations = Translations.getRb();
    }

    public CourseManager getCourseManager() {
        return cm;
    }

    @Override
    public int getRowCount() {
        int rowCount = 0;
        if (course != null) {
            rowCount = users.size();
        }
        return rowCount;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if (columnIndex == 0) {
            return String.class;
        } else {
            return Boolean.class;
        }
    }

    @Override
    public int getColumnCount() {
        int clmCount = 0;
        if (course != null) {
            clmCount = events.size() + 1;
        }
        return clmCount;
    }

    @Override
    public String getColumnName(int columnIndex) {
        if (course == null || columnIndex == 0) {
            return "";
        }
        Event event = events.get(columnIndex - 1);
        return event.getEventNumber().toString();
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        User user = null;
        user = users.get(rowIndex);
        if (columnIndex == 0) {
            return user.getName() + " (" + user.getUco() + ")";
        }
        return attendees.get(events.get(columnIndex - 1)).contains(user);
    }

    @Override
    public void setValueAt(Object value, int row, int col) {
        try {
            User user = users.get(row);
            Event event = events.get(col - 1);
            if (value.equals(true)) {
                cm.addAttendee(event, user);
            } else {
                cm.cancelAttendance(event, user);
            }
            attendees.put(event, new ArrayList<User>(cm.getAttendees(event)));
            fireTableCellUpdated(row, col);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, translations.getString("err_a_att"));
        } catch (UserNotEnrolledException ex) {
            JOptionPane.showMessageDialog(null, translations.getString("err_a_notenroll"));
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        if (columnIndex == 0) {
            return false;
        } else {
            return true;
        }
    }

    public void setCourse(int courseNumber) {
        try {
            this.course = cm.getCourseById(courseNumber);
            users = new ArrayList<User>(cm.getEnrolled(course));
            events = new ArrayList<Event>(cm.getEventsForCourse(course));
            attendees = new HashMap<Event, List<User>>(events.size());
            for (Event event : events) {
                attendees.put(event, new ArrayList<User>(cm.getAttendees(event)));
            }
            fireTableStructureChanged();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, translations.getString("err_a_list"));
        }
    }
}